-- UIAirShipSkillDesc
-- Create by cheny3
-- 秘宝说明界面

--UIWorkshopDesc继承自Layer
UIAirShipSkillDesc = class("UIAirShipSkillDesc", function()
    return cc.Layer:create();
end);

function UIAirShipSkillDesc.create(skillId)
    return UIAirShipSkillDesc.new(skillId);
end

--构造函数
function UIAirShipSkillDesc:ctor(skillId)
    self:setName("UIAirShipSkillDesc");

    self.skillId = skillId;

    -- 初始化/复用飞船说明界面
    local node = cc.CSLoader:createNode("layout/sky_city/AirShipDesc.csb");
    self:addChild(node);
    self.node = node;

    -- 初始化
    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    -- 重绘
    self:redraw();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 适配
    self:resize();
end

-- 重绘
function UIAirShipSkillDesc:redraw()
    -- 标题
    local combatTitleLabel = findChildByName(self.node,"CT/combat_title");
    local combatDescLabel  = findChildByName(self.node,"CT/combat_desc");
    local selectTitleLabel = findChildByName(self.node,"CT/select_title");
    local selectDescLabel  = findChildByName(self.node,"CT/select_desc");
    local team_panel       = findChildByName(self.node, "CT/team_panel");
    team_panel:setVisible(true);

    -- 获取秘宝信息
    local name   = SkyShipSkillM.query(self.skillId, "name");
    local desc   = SkyShipSkillM.getShipSkillDesc(self.skillId);
    local detail = SkyShipSkillM.getShipSkillDetail(self.skillId);
    local group  = SkyShipSkillM.query(self.skillId, "group");
    local getType = SkyShipSkillM.query(self.skillId, "get_type");
    local hasOwn = SkyShipSkillM.hasOwn(self.skillId);
    local isActived = SkyShipSkillM.isSkillActived(self.skillId);
    local selectDesc = iif(FormulaM.invoke("IS_HALLOWS_AIRSHIP_SKILL", self.skillId), getLocStr("airship_skill_desc3"), getLocStr("airship_skill_desc"))

    TextStyleM.setTextStyle(combatTitleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    TextStyleM.setTextStyle(selectTitleLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE, nil, nil, nil, nil, nil, TextStyleM.TEXT_SIZE_SMALL);
    TextStyleM.setTextStyle(combatDescLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, nil, nil, nil, nil, nil, TextStyleM.TEXT_SIZE_SMALL);
    TextStyleM.setTextStyle(selectDescLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_RED, nil, nil, nil, nil, nil, TextStyleM.TEXT_SIZE_SMALL);

    -- 未获得或未激活
    if not hasOwn or not isActived then
        TextStyleM.setTextStyle(selectTitleLabel,TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY, nil, nil, nil, nil, nil, TextStyleM.TEXT_SIZE_SMALL);
    end

    -- 秘宝说明
    combatDescLabel:setString(desc);
    -- 秘宝细节
    selectTitleLabel:setString(detail);
    -- 通用说明
    selectDescLabel:setString(selectDesc);

    -- 未获得
    if not hasOwn then
        -- 名称后显示未获得
        name = name..getLocStr("not_captured_airship_skill");

        -- 显示秘宝获得途径
        if getType then
            selectDescLabel:setString(getLocStr("airship_skill_get_type_" .. getType));
        else
            selectDescLabel:setString(getLocStr("airship_skill_desc2"));
        end
    end

    -- 秘宝名称
    local newCombatTitleLabel = applyString(combatTitleLabel, name);

    -- 绘制冈布奥队伍
    local itemHeight = 50;
    local itemWidth  = 220;
    local itemGap    = 40;
    local panelWidth = team_panel:getContentSize().width;
    local panelHeight = math.ceil(#group/2) * itemHeight;
    local kerning    = 0;

    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning = -1;
    else
        kerning = -2;
    end

    team_panel:setContentSize(cc.size(panelWidth, panelHeight));
    for i, id in pairs(group) do
        local itemNode = cc.CSLoader:createNode("layout/hero/HeroInfoItem.csb");

        local posX, posY;
        if i % 2 == 0 then
            posX = panelWidth/2 + itemWidth/2 + itemGap/2;
        else
            posX = panelWidth/2 - itemWidth/2 - itemGap/2;
        end

        posY = panelHeight - itemHeight/2 - itemHeight*(math.floor((i + 1)/2) - 1);

        itemNode:setPosition(posX, posY);
        team_panel:addChild(itemNode);

        local iconImg   = findChildByName(itemNode, "icon");
        local nameLabel = findChildByName(itemNode, "name");

        -- 某只宠物是否出战中
        if not SkyCombatM.isInArmy(id) then
            -- 如果该冈布奥未激活且没有，用灰色显示
            local icon = PetM.getDefaultIcon(id);
            if not PetM.getMyPet(id) then
                -- 冈布奥轮廓图
                iconImg:loadTexture(getHeroSilhouetteSmallIconPath(icon));
            else
                -- 冈布奥图标
                iconImg:loadTexture(getSmallHeroIconPath(icon));
                iconImg:setScale(1.0);

                -- 没出战，置灰
                setGrayTransMode(iconImg, true);
            end

            -- 冈布奥名称
            local name = PetM.query(id, "name");
            TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BEIGE, false, kerning);
            nameLabel:setString(name);
        else
            -- 冈布奥图标
            local icon = PetM.getDefaultIcon(id);
            iconImg:loadTexture(getSmallHeroIconPath(icon));
            iconImg:setScale(1.0);

            -- 冈布奥名称
            local name = PetM.query(id, "name");
            TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BEIGE, false, kerning);
            nameLabel:setString(name);
        end
    end

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL,
        { newCombatTitleLabel, combatDescLabel, selectTitleLabel, selectDescLabel, team_panel },
        { AlignM.DEFAULT_GAP, AlignM.DEFAULT_GAP, AlignM.DEFAULT_GAP, AlignM.DEFAULT_GAP });
end

-- 注册点击事件
function UIAirShipSkillDesc:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("ship_return");
            UIMgr.getCurrentScene():removeFormByName("UIAirShipSkillDesc");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册点击任意地方关闭当前窗口事件
    local btnBgBack = findChildByName(self.node, "bg/bgImg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIAirShipSkillDesc");
        end
    end
end

-- 适配
function UIAirShipSkillDesc:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end


